package com.tarzan.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarzan.api.model.request.role.SysRoleBindingMenuReq;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.common.core.exception.ErrorCodesEnum;
import com.tarzan.system.dao.SysRoleMenuMapper;
import com.tarzan.system.entity.SysRoleMenu;
import com.tarzan.system.service.ISysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色和菜单关联表 服务实现类
 * </p>
 *
 * @author liujinshan
 * @since 2020-07-09
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {


    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 根据角色id获取对应的菜单权限
     *
     * @param roleId
     * @return java.util.List<com.jinshan.happy.vo.response.SysRolePermissionsResp>
     * @Date 2020/10/10 15:04
     * @Author liujinshan
     **/
    @Override
    public List<Integer> getMenuInfoByRoleId(Long roleId) {

        if (null == roleId) {
            throw  new BusinessException(ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.code(),ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.msg());
        }
        List<Integer> list = sysRoleMenuMapper.getMenuInfoByRoleId(roleId);

        return list;
    }

    /**
     * 给角色分配菜单权限
     *
     * @param sysRoleBindingMenuReq
     * @return void
     * @Date 2020/10/12 9:18
     * @Author liujinshan
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(SysRoleBindingMenuReq sysRoleBindingMenuReq) {
        for (int i = 0;i<sysRoleBindingMenuReq.getMenuIds().size();i++) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(sysRoleBindingMenuReq.getRoleId());
            sysRoleMenu.setMenuId(Long.valueOf(sysRoleBindingMenuReq.getMenuIds().get(i)));
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
    }
}
